package coml.jiyun.hp.xaizai.fragment;

import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import coml.jiyun.hp.xaizai.R;

/**
 * A simple {@link Fragment} subclass.
 */
public class DownLoadFragment extends Fragment {

    private Button button;
    private ProgressBar progressBar;
    private TextView textView;

    public DownLoadFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_download, container, false);
        initView(inflate);
        return inflate;
    }

    private void initView(View inflate) {
        progressBar = inflate.findViewById(R.id.pb);
        textView = inflate.findViewById(R.id.tv);
        button = inflate.findViewById(R.id.btn);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                download();
            }
        });
    }

    private void download() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    for (int i = 0; i < 101; i++) {
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        progressBar.setProgress(i);
                    }
                }
            }).start();
        Toast.makeText(getActivity(), "下载完成", Toast.LENGTH_SHORT).show();
        }
    }
